package com.yikes.srb.controller.web;

import com.yikes.srb.common.result.PageResult;
import com.yikes.srb.common.result.Result;
import com.yikes.srb.leadingModle.form.borrowinfo.BorrowInfoAddForm;
import com.yikes.srb.leadingModle.form.borrowinfo.BorrowInfoApprovalForm;
import com.yikes.srb.leadingModle.form.borrowinfo.BorrowInfoEditForm;
import com.yikes.srb.leadingModle.form.borrowinfo.BorrowInfoQuery;
import com.yikes.srb.leadingModle.vo.BorrowInfoVO;
import com.yikes.srb.service.BorrowInfoService;
import com.yikes.srb.web.utils.jwt.JwtUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 * 借款信息表 前端控制器
 * </p>
 *
 * @author guanrong.yin
 * @since 2024-12-03
 */
@RequiredArgsConstructor
@Tag(name = "BorrowInfoController", description = "借款信息表")
@RestController
@RequestMapping("/admin/borrow_info")
public class BorrowInfoController {

    private final BorrowInfoService service;


    @Operation(description = "分页查询")
    @GetMapping("/list_page")
    public Result<PageResult<BorrowInfoVO>> listPage(BorrowInfoQuery query) {

        return Result.ok(service.listPage(query));
    }

    @Operation(description = "根据id查询单条信息")
    @GetMapping("/info/{id}")
    public Result<BorrowInfoVO> infoId(@PathVariable("id") Long id) {

        return Result.ok(service.infoId(id));
    }

    @Operation(description = "新增")
    @PostMapping("/create")
    public Result create(@Valid @RequestBody BorrowInfoAddForm form) {

        service.create(form);
        return Result.ok();
    }

    @Operation(description = "编辑")
    @PutMapping("/edit")
    public Result edit(@Valid @RequestBody BorrowInfoEditForm form) {

        service.edit(form);
        return Result.ok();
    }

    @Operation(description = "删除")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Long id) {

        service.del(id);
        return Result.ok();
    }

    @Operation(description = "获取借款额度")
    @GetMapping("/auth/getBorrowAmount")
    public Result getBorrowAmount(HttpServletRequest request) {
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

        BigDecimal borrowAmount = service.getBorrowAmount(userId);
        return Result.ok(borrowAmount);
    }

    @Operation(description ="提交借款申请")
    @PostMapping("/auth/save")
    public Result save(@RequestBody BorrowInfoAddForm form, HttpServletRequest request) {

        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

        service.saveBorrowInfo(form, userId);
        return Result.ok("提交成功");
    }

    @Operation(description = "获取借款申请审批状态")
    @GetMapping("/auth/getBorrowInfoStatus")
    public Result getBorrowerStatus(HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

        Integer status = service.getStatusByUserId(userId);
        return Result.ok(status);
    }

    @Operation(description = "获取借款信息")
    @GetMapping("/show/{id}")
    public Result show(@PathVariable Long id) {

        Map<String, Object> borrowInfoDetail = service.getBorrowInfoDetail(id);
        return Result.ok(borrowInfoDetail);
    }

    @Operation(description = "审批借款信息")
    @PostMapping("/approval")
    public Result approval(@RequestBody BorrowInfoApprovalForm form) {

        service.approval(form);
        return Result.ok("审批完成");
    }

}
